package hoang.hut.libs.adapter;

import hoang.hut.libs.R;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemAdapter extends ArrayAdapter<Item>{
	
	private ArrayList<Item> array;
	@SuppressWarnings("unused")
	private Context context;
	LayoutInflater layoutInflater;
	

	public ItemAdapter(Context context, ArrayList<Item> objects) {
		super(context, R.layout.layout_item, objects);
		
		this.context = context;
		this.array = objects;
		layoutInflater = LayoutInflater.from(getContext());
		
	}
	
	@Override
	public int getCount() {
		return super.getCount();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder;

		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.layout_item, null);
			holder = new ViewHolder();
			holder.imgThumnail = (ImageView) convertView.findViewById(R.id.imgThumnail);
			holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
			convertView.setTag(holder);
		}

		holder = (ViewHolder) convertView.getTag();

		Item item = array.get(position);

		holder.imgThumnail.setImageBitmap(item.getPicture());
		holder.tvName.setText(item.getName());

		return convertView;
		
	}
	
	static class ViewHolder {
		ImageView imgThumnail;
		TextView tvName;
	}
}
